<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="合同名称" prop="contarctName">
        <el-input
          v-model="queryParams.contarctName"
          placeholder="请输入合同名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同编号" prop="contarctCode">
        <el-input
          v-model="queryParams.contarctCode"
          placeholder="请输入合同编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="contarctList" @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="id"/>
      <el-table-column label="合同名称" align="center" prop="contarctName"/>
      <el-table-column label="合同状态" align="center" prop="contarctStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.contarct_status" :value="scope.row.contarctStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="合同编号" align="center" prop="contarctCode"/>
      <el-table-column label="物管合同号" align="center" prop="propertyCode"/>
      <el-table-column label="签订日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁开始期限" align="center" prop="contractStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁结束期限" align="center" prop="contractEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁方" align="center" prop="companyId"/>
      <el-table-column label="客户名称" align="center" prop="contactName"/>
      <el-table-column label="联系电话" align="center" prop="contactMobile"/>
      <el-table-column width="150" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['contarct:contarct:edit']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['contarct:contarct:edit']"
          >审批
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同对话框 -->
    <el-dialog title="审核" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审批" prop="contarctStatus">
          <el-radio-group v-model="form.contarctStatus">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="1">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="原因" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入原因"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {
    listContarct,
    getContarctInfo,
    delContarct,
    addContarct,
    updateContarct,
    applyContarct,
    rejectContarct
  } from "@/api/contarct/contarct";

  export default {
    name: "Contarct",
    dicts: ['sys_status', 'contarct_status', 'rent_month_type', 'unit_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 合同表格数据
        contarctList: [],
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          contarctCode: null,
          rentCode: null,
          propertyCode: null,
          contarctName: null,
          companyId: null,
          companyName: null,
          contactAddress: null,
          contactName: null,
          contactMobile: null,
          contractStartTime: null,
          contractEndTime: null,
          createTime: null,
          freeStarttime: null,
          freeEndTime: null,
          roomids: null,
          status: null,
          contarctPath: null,
          updateTime: null,
          signBy: null,
          signTime: null,
          bondfee: null,
          bondmouth: null,
          allarea: null,
          contarctStatus: "1",
          contactEmail: null,
          rentMonthType: null,
          rentfee: null,
          rentfeeUnit: null,
          rentfeeDay: null,
          rentfeeMonth: null,
          propertyfee: null,
          propertyfeeUnit: null,
          properDay: null,
          properMonth: null
        },
        // 表单参数
        form: {
          applyContractId: undefined,
          remark: undefined
        },
        // 表单校验
        rules: {
          contarctStatus:[
            {required: true, message: "审批状态不能为空！", trigger: 'change'}
          ]
        }
      };
    },
    activated() {
      this.getList();
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询合同列表 */
      getList() {
        this.loading = true;
        listContarct(this.queryParams).then(response => {
          this.contarctList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          contarctCode: null,
          rentCode: null,
          propertyCode: null,
          contarctName: null,
          companyId: null,
          companyName: null,
          contactAddress: null,
          contactName: null,
          contactMobile: null,
          contractStartTime: null,
          contractEndTime: null,
          createTime: null,
          freeStarttime: null,
          freeEndTime: null,
          roomids: null,
          status: null,
          contarctPath: null,
          updateTime: null,
          signBy: null,
          signTime: null,
          createBy: null,
          updateBy: null,
          bondfee: null,
          bondmouth: null,
          allarea: null,
          contarctStatus: null,
          contactEmail: null,
          rentMonthType: null,
          rentfee: null,
          rentfeeUnit: null,
          rentfeeDay: null,
          rentfeeMonth: null,
          propertyfee: null,
          propertyfeeUnit: null,
          properDay: null,
          properMonth: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加合同";
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除合同编号为"' + ids + '"的数据项？').then(function () {
          return delContarct(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('contarct/contarct/export', {
          ...this.queryParams
        }, `contarct_${new Date().getTime()}.xlsx`)
      },
      //合同详情
      handleDetail(row) {
        this.conductionData.contractId = row.id || this.ids[0]
        this.$router.push({
          path: "/contarct/ContarctDetails"
        });
      },
      /** 审核按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form.applyContractId = row.id
        this.open = true;
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.contarctStatus === 2) {//审批通过
              if (this.form.applyContractId != null) {
                applyContarct(this.form.applyContractId).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                  return
                });
              }
            }
            if(this.form.contarctStatus === 1){//审批驳回
              if (this.form.applyContractId != null) {
                rejectContarct(this.form.applyContractId,this.form.remark||null).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                  return
                });
              }
            }
          }
        });
      },
    }
  };
</script>
